
var apiurl = "http://wk.tinyask.cn/";
var request = function(url = '', data = {}, method = 'GET', header = {}, showLoad = true) {
    return new Promise(function(resolve, reject) {
        if (showLoad) uni.showLoading({ 'title': '加载中...' })
        uni.request({
            url: apiurl + url,
            method: method,
            data: data,
            header: header,
            success: function(result) {
				console.log(result)
                var { data } = result
				var { code } = data;
                var message = data.msg;
                if (code == 500) {
                    uni.showToast({
                        title: message,
                        icon: 'none',
                        duration: 2000
                    })
                }
                uni.hideLoading()
                resolve(result)
            },
            fail: function(res) {
                reject(res)
                uni.hideLoading()
            },
            complete: function(res) {
                uni.hideLoading()
            }
        });
    })
}


const api = {
    xcxrequest: async function(
        url = "",
        data = {},
        method = "GET",
        need_login = false,
        loading = true
    ) {
        console.log(url);
        var header = {}
		// this.checkLogin()
			
		var token = uni.getStorageSync("token");
		console.log('请求获取token:'+token)
		header.token = token;
        var result = await request(url, data, method, header, loading);
		// console.log(result, '======')
		if(result.data.code == 401){
			// 登录失效。重新登录
			uni.setStorageSync("token", "");
			this.checkLogin();
		}

        result["result"] = result["data"];
        return result;
    },

    checkLogin: async function() {
        var token = uni.getStorageSync("token");
		console.log('---------------checkLogin token ---- ');
		console.log(token);
        if(!token){
			// 这里自动取授权。
			var url = "http://wk.tinyask.cn/kucun/QiyeWork/login"
			location.href = url;
		}
    },
	getUser: function() {
	    return this.xcxrequest("/kucun/QiyeWork/getUser", {}, "POST", false, true);
	},
	getAgentJsConfig: function(data = {}) {
	    return this.xcxrequest("/kucun/QiyeWork/getAgentJsConfig", data, "POST", false, true);
	},
	getCorpJsConfig: function(data = {}) {
	    return this.xcxrequest("/kucun/QiyeWork/getCorpJsConfig", data, "POST", false, true);
	},
	getLoginUrl: function() {
	    return this.xcxrequest("/kucun/QiyeWork/getLoginUrl", {}, "POST", false, true);
	},
	config: function() {
	    return this.xcxrequest("/kucun/Config/config", {}, "GET", false, true);
	},
	shopList: function() {
	    return this.xcxrequest("/kucun/shop/list", {}, "GET", false, true);
	},
	shopAdd: function(data) {
	    return this.xcxrequest("/kucun/shop/add", data, "post", false, true);
	},
	categoryList: function() {
	    return this.xcxrequest("/kucun/category/list", {}, "GET", false, true);
	},
	categoryAdd: function(data) {
	    return this.xcxrequest("/kucun/category/add", data, "post", false, true);
	},

	productList: function(data) {
	    return this.xcxrequest("/kucun/product/list", data, "GET", false, false);
	},
	productAdd: function(data) {
	    return this.xcxrequest("/kucun/product/add", data, "post", false, true);
	},
	productStockList: function(data) {
	    return this.xcxrequest("/kucun/product_stock/list", data, "POST", false, false);
	},
	productStockAdd: function(data) {
	    return this.xcxrequest("/kucun/product_stock/add", data, "post", false, true);
	},
	homeData: function(data) {
	    return this.xcxrequest("/kucun/Debash/homeData", data, "post", false, true);
	},
}
export default api;